package kr.hkit.powerjava.chapter26.service;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;

import kr.hkit.powerjava.chapter26.dao.EmployeeDAO;
import kr.hkit.powerjava.chapter26.dto.Employee;

public class EmployeeService extends AbsService<Employee> {


	public EmployeeService(Scanner sc, Connection connection) {
		super(sc, connection);
		dao = new EmployeeDAO(connection);
	}

	@Override
	public Employee inputItemInfo(boolean gubun) {
		System.out.println("사원번호 사원명");

		Employee emp = new Employee(sc.nextInt(), sc.next().trim()); 

		if (gubun) {
			System.out.println("직책 직속번호 급여 부서번호");
		
			emp.setTitle(sc.next());
			emp.setManager(sc.nextInt());
			emp.setSalary(sc.nextInt());
			emp.setDno(sc.nextInt());
		}
		return emp;
	}

	@Override
	public void prnSubMenu() {

		ArrayList<Employee> list = null; 

		System.out.println("1. 이름순 정렬 2. 급여순 정렬 3. 사원번호별 정렬");
		try {
			list = dao.listItems();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		int res = sc.nextInt();
		switch (res) {
			case 1:
				prnItemList(new Comparator<Employee>() { // 이름순
					@Override
					public int compare(Employee o1, Employee o2) {
						return o1.getEmpName().hashCode()
								- o2.getEmpName().hashCode();
					}
				}, list);
				break;
			case 2:
				prnItemList(new Comparator<Employee>() {
					@Override
					public int compare(Employee o1, Employee o2) {
						return o2.getSalary() - o1.getSalary();
					}
				}, list);
				break;
			default:
				prnItemList(null, list);
		}
	}


	/*private void prnItemList(Comparator<Employee> comparator) {
		if (comparator == null) { // 기본정렬. 사원번호순
			Collections.sort(list);
		} else {
			Collections.sort(list, comparator);
		}
		prnlist(list);
	}

	private void prnlist(ArrayList<Employee> list) { //정렬한거 출력용
		for (Employee e : list) {
			System.out.println(e);
		}
	}*/
}
